<div id="projectGrid"></div>
<script>
	$(function() {
		var columns = [{
			title : '项目名称',
			name : 'name',
			width : 150
		}, {
			title : '开发者',
			name : 'developers',
			width : 210,
			render : function(item, name, index) {
				var developers = [];
				$.each(item[name], function() {
					developers.push(this.developer.name);
				});
				return developers.join(',');
			}
		}, {
			title : '集成工具',
			name : 'tools',
			width : 210,
			render : function(item, name, index) {
				var tools = [];
				$.each(item[name], function() {
					tools.push('<a href="' + this.toolRequestAddress + '" target="_">' + this.toolConfiguration.name + '</a>');
				});
				return tools.join(',');
			}
		}, {
			title : '项目状态',
			name : 'projectStatus',
			width : 90,
			render : function(item, name, index) {
				switch(item[name]) {
					case 'SUCCESS':
						return '&nbsp;&nbsp;<span class="glyphicon glyphicon-ok" style="color:#5CB85C;"></span>';
					case 'INTEGRATION_TOOL_FAILURE':
						return '&nbsp;&nbsp;<span class="glyphicon glyphicon-remove" style="color:#D9534F;"></span>&nbsp;&nbsp;<a href="#" onclick="againIntegeration(' + item.id + ', this)">重新整合</a>';
					case 'INTEGRATION_TOOL':
						return '&nbsp;&nbsp;<div data-id="' + item.id + '" data-role="projectLoading"　title="正在整合工具" style="position: relative;top: -10px;" onclick="showProjectMsg(' + item.id + ', this)"><img alt="正在整合工具" src="images/loading.gif" style="width: 35px; height:35px;"></img></div>';
					default:
						return item[name]
				}
			}
		}, {
			title : '创建时间',
			name : 'projectCreateDate',
			width : 150
		}, {
			title : '操作',
			name : 'projectSavePath',
			width : 200,
			render : function(item, name, index) {
				return '<button class="btn btn-primary btn-sm" style="padding: 4px 10px;" type="button" onclick="showProjectDetail(' + item.id + ')"><span class="glyphicon glyphicon-search"><span>详细</button>&nbsp;&nbsp;&nbsp;<button class="btn btn-danger btn-sm" style="padding: 4px 10px;"  type="button" onclick="deleteProject(' + item.id + ')"><span class="glyphicon glyphicon-remove"><span>删除</button>';
			}
		}];
		$('#projectGrid').grid({
			identity : 'id',
			columns : columns,
			querys : [{
				title : '项目名称',
				value : 'name'
			}],
			url : 'project/pagingquery'
		}).on('complate', function() {
			$(this).find('[data-role="projectLoading"]').each(function() {
				var id = this.getAttribute('data-id')
				var $td = $(this).closest('td');
				var intervalId = setInterval(function() {
					$.get('project/detail/' + id).done(function(project) {
						switch(project.projectStatus) {
							case 'SUCCESS':
								$td.html('<span class="glyphicon glyphicon-ok" style="color:#5CB85C;"></span>');
								clearInterval(intervalId);
								break;
							case 'INTEGRATION_TOOL_FAILURE':
								$td.html('&nbsp;&nbsp;<span class="glyphicon glyphicon-remove" style="color:#D9534F;"></span>&nbsp;&nbsp;<a href="#" onclick="againIntegeration(' + id + ', this)">重新整合</a>');
								clearInterval(intervalId);
								break;
							default:
								break;
						}
					});
				}, 2000);
			});
		});
	});

	var showProjectMsg = function(id, obj) {
		$.get('pages/cis/project-real-time.html').done(function(data) {
			var dialog = $(data);
			dialog.modal({
				keyboard : false
			}).on('hidden.bs.modal', function() {
				$(this).remove();
			});
			var modalBody = dialog.find('.modal-body');
			var intervalId = setInterval(function() {
				$.ajax({
					type : 'GET',
					url : 'project/process/' + id,
					async : false
				}).done(function(data) {
					if (!data.result) {
						clearInterval(intervalId);
						$.get('project/detail/' + id).done(function(project) {
							var projectStatus = $(obj).closest('td');
							switch(project.projectStatus) {
								case 'SUCCESS':
									projectStatus.html('<span class="glyphicon glyphicon-ok" style="color:#5CB85C;"></span>');
									break;
								case 'INTEGRATION_TOOL_FAILURE':
									projectStatus.html('&nbsp;&nbsp;<span class="glyphicon glyphicon-remove" style="color:#D9534F;"></span>&nbsp;&nbsp;<a href="#" onclick="againIntegeration(' + id + ', this)">重新整合</a>');
									break;
								default:
									break;
							}
						});
						modalBody.html(data.msg);
						modalBody.scrollTop(modalBody[0].scrollHeight);
					} else {
						modalBody.html(data.msg);
						modalBody.scrollTop(modalBody[0].scrollHeight);
					}
				});
			}, 1000);
		});
	}
	var showProjectDetail = function(id) {
		$.get('project/detail/' + id).done(function(project) {
			$.get('pages/cis/project-detail.html').done(function(data) {
				var dialog = $(data);
				dialog.find('#name').html(project.name);
				dialog.find('#groupId').html(project.projectDetail.groupId);
				dialog.find('#artifactId').html(project.projectDetail.artifactId);
				var projectStatus = dialog.find('#projectStatus');
				switch(project.projectStatus) {
					case 'SUCCESS':
						projectStatus.html('<span style="font-weight:bold; color:#5CB85C;">创建成功</span>');
						break;
					case 'INTEGRATION_TOOL_FAILURE':
						projectStatus.html('<span style="font-weight:bold; color:#D9534F;">创建失败</span>');
						break;
					case 'INTEGRATION_TOOL':
						projectStatus.html('正在整合工具');
						break;
					default:
						projectStatus.html(project.projectStatus);
						break;
				}
				//dialog.find('#projectStatus').html(project.projectStatus);
				dialog.find('#projectSavePath').html(project.projectDetail.projectSavePath);
				dialog.find('#createDate').html(project.projectCreateDate);
				var devlopers = [];
				$.each(project.developers, function() {
					devlopers.push('&nbsp;&nbsp;' + this.developer.name);
				});
				dialog.find('#devlopers').html(devlopers.join(','));
				var tools = [];
				$.each(project.tools, function() {
					var html = [];
					var toolStatus = '';
					switch(this.status) {
						case 'SUCCESS':
							toolStatus = '<span style="font-weight:bold; color:#5CB85C;">整合成功</span>';
							break;
						case 'FAILURE':
							toolStatus = '<span style="font-weight:bold; color:#D9534F;">整合失败</span>';
							break;
						default :
							toolStatus = '<span style="font-weight:bold; color:#D9534F;">正在整合工具</span>';
							break;
					}
					html.push('<div class="panel panel-default"><div class="panel-heading">' + this.toolConfiguration.name + '(' + toolStatus + ')</div><table class="table"><thead><tr><th class="first">创建过程</th><th class="second">时间</th><th class="third">结果</th></tr></thead><tbody>');
					$.each(this.toolInterfaceImplements, function() {
						var result = '';
						if (this.success) {
							result = '<span style="font-weight:bold;color:#5CB85C">成功</span>';
						} else {
							result = '<span style="font-weight:bold;color:#D9534F">失败' + (this.record == null ? '' : '(' + this.record + ')') + '</span>';
						}
						html.push('<tr><td class="first">' + this.toolInterface + '</td><td class="second">' + this.executeDate + '</td><td class="third">' + result + '</td></tr>');
					});
					html.push('</tbody></table></div>');
					tools.push(html.join(''));
				});
				tools.push('<div class="panel panel-default"><div class="panel-heading">邮件发送</div><table class="table"><thead><tr><th class="first">用户名</th><th class="second">邮箱</th><th class="third">发送结果</th></tr></thead><tbody>');
				$.each(project.developers, function() {
					var result = '';
					if (this.noticeSuccess) {
						result = '<span style="font-weight:bold;color:#5CB85C">成功</span>';
					} else {
						result = '<span style="font-weight:bold;color:#D9534F">失败' + (this.record == null ? '' : '(' + this.record + ')') + '</span>';
					}
					tools.push('<tr><td class="first">' + this.developer.name + '</td><td class="second">' + this.developer.email + (this.noticeSuccess ? '' : '&nbsp;&nbsp;<a href="#" onclick="againSendemail(' + id + ')">重新发送</a>') + '</td><td class="third">' + result + '</td></tr>');
				});
				tools.push('</div>')
				dialog.find('#tools').html(tools.join(''));
				dialog.modal({
					keyboard : false
				}).on({
					'hidden.bs.modal' : function() {
						$(this).remove();
					}
				});
			});
		});
	}
	var deleteProject = function(id) {
		$('#projectGrid').confirm({
			content : '确定要删除该项目吗?',
			callBack : function() {
				$.post('project/remove/' + id).done(function(result) {
					if (result) {
						$('#projectGrid').message({
							type : 'success',
							content : '删除成功'
						});
						$('#projectGrid').grid('refresh');
					} else {
						$('#projectGrid').message({
							type : 'error',
							content : '删除失败'
						});
					}
				});
			}
		});
	}
	var againIntegeration = function(id, obj) {
		var $td = $(obj).closest('td');
		$.get('project/again-integeration/' + id).done(function() {
			$td.html('&nbsp;&nbsp;<div title="正在整合工具" style="position: relative;top: -10px;" onclick="showProjectMsg(' + id + ', this)"><img alt="正在整合工具" src="images/loading.gif" style="width: 35px; height:35px;"></img></div>');
			var intervalId = setInterval(function() {
				$.get('project/detail/' + id).done(function(project) {
					switch(project.projectStatus) {
						case 'SUCCESS':
							$td.html('<span class="glyphicon glyphicon-ok" style="color:#5CB85C;"></span>');
							clearInterval(intervalId);
							break;
						case 'INTEGRATION_TOOL_FAILURE':
							$td.html('&nbsp;&nbsp;<span class="glyphicon glyphicon-remove" style="color:#D9534F;"></span>&nbsp;&nbsp;<a href="#" onclick="againIntegeration(' + id + ', this)">重新整合</a>');
							clearInterval(intervalId);
							break;
						default:
							break;
					}
				});
			}, 2000);
		});
	}
	var againSendemail = function(id) {
		$.get('project/again-sendemail/' + id).done(function() {
			$('.modal-dialog .modal-body').message({
				type : 'success',
				content : '重新发送成功'
			});
		});
	}
</script>
